package com.loong.system.handler;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.loong.common.domain.Pager;
import com.loong.common.utils.TransformUtils;
import com.loong.system.business.SystemFeedbackBusiness;
import com.loong.system.common.backend.param.SystemFeedbackQueryParam;
import com.loong.system.common.backend.param.SystemFeedbackReplyParam;
import com.loong.system.common.backend.result.SystemFeedbackResult;
import com.loong.system.common.forehead.param.DeleteFeedbackForeheadParam;
import com.loong.system.common.forehead.param.SystemFeedbackForeheadParam;
import com.loong.system.common.forehead.param.SystemFeedbackForeheadQueryParam;
import com.loong.system.common.forehead.result.SystemFeedbackForeheadResult;
import com.loong.system.model.SystemFeedback;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.xml.crypto.dsig.Transform;
import java.util.Date;
import java.util.List;

/**
 * @author LiuQun
 * @date 2020-09-28 9:23
 */
@Service
public class SystemFeedbackHandler {

    @Resource
    private SystemFeedbackBusiness feedbackBusiness;

    /**
     * 前台新增用户反馈
     * @param feedbackParam 反馈参数
     */
    public void foreheadSaveFeedback(SystemFeedbackForeheadParam feedbackParam){
        feedbackParam.setCreateTime(new Date());
        feedbackBusiness.save(TransformUtils.transform(feedbackParam,SystemFeedback.class));
    }

    /**
     * 前台批量删除用户反馈
     * @param param 反馈id集合和用户id封装的参数
     */
    public void foreheadBatchDeleteFeedback(DeleteFeedbackForeheadParam param){
         List<Long> idList = param.getFeedbackIdList();
        for (Long id : idList) {
            feedbackBusiness.deleteFeedbackByIdParam(param.getUserId(),id);
        }
    }

    /**
     * 前台分页查询用户反馈
     * @param param 查询参数
     * @return 分页结果
     */
    public Pager<SystemFeedbackForeheadResult> foreheadPageQueryFeedback(SystemFeedbackForeheadQueryParam param){
        Pager<SystemFeedback> pager = feedbackBusiness.pageQuery(param, () -> feedbackBusiness.findFeedbackByParam(param));
        return TransformUtils.transformPage(pager,SystemFeedbackForeheadResult.class);
    }

    /**
     * 前台用户对回复的反馈进行评价
     * @param param 反馈参数
     */
    public void foreheadAppraiseFeedback(SystemFeedbackForeheadParam param){
        param.setLastUpdateTime(new Date());
        feedbackBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param,SystemFeedback.class));
    }

    /**
     * 前台更新用户反馈
     * @param param 反馈参数
     */
    public void foreheadUpdateFeedback(SystemFeedbackForeheadParam param){
        param.setLastUpdateTime(new Date());
        feedbackBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param,SystemFeedback.class));
    }

    /**
     * 后台更新用户反馈的回复状态
     * @param replyParam 回复反馈封装的参数
     */
    protected void updateFeedbackRepliedStatus(SystemFeedbackReplyParam replyParam){
        feedbackBusiness.updateByPrimaryKeySelective(TransformUtils.transform(replyParam,SystemFeedback.class));
    }

    /**
     * 后台分页查询用户反馈
     * @param queryParam 查询参数
     * @return 分页结果
     */
    public Pager<SystemFeedbackResult> pageQueryFeedback(SystemFeedbackQueryParam queryParam){
        SystemFeedbackForeheadQueryParam param = TransformUtils.transform(queryParam, SystemFeedbackForeheadQueryParam.class);
        Pager<SystemFeedback> feedbackPager = feedbackBusiness.pageQuery(queryParam, () -> feedbackBusiness.findFeedbackByParam(param));
        return TransformUtils.transformPage(feedbackPager,SystemFeedbackResult.class);
    }

    /**
     * 后台根据反馈类型id查询是否存在对应的用户反馈
     * @param feedbackTypeId 反馈类型id
     * @return 是否存在用户反馈
     */
    protected boolean existsFeedbackByFeedbackType(Long feedbackTypeId){
        SystemFeedbackForeheadQueryParam param = SystemFeedbackForeheadQueryParam.builder().feedbackTypeId(feedbackTypeId).build();
        List<SystemFeedback> feedbackList = feedbackBusiness.findFeedbackByParam(param);
        //若集合为空，表示不存在该反馈类型的用户反馈，结果返回false
        return !CollectionUtils.isEmpty(feedbackList);
    }

}
